package io.geekidea.boot.common.controller;

import io.geekidea.boot.config.properties.LocalFileProperties;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Controller
public class StaticResourceController {

    @jakarta.annotation.Resource
    private ResourceLoader resourceLoader;
    @jakarta.annotation.Resource
    private LocalFileProperties localFileProperties;

    @GetMapping("/file/{date}/{filename}")
    public ResponseEntity<Resource> serveFile(@PathVariable String date, @PathVariable String filename) {
        Resource resource = resourceLoader.getResource("file:" + localFileProperties.getUploadPath() + "/" + date + "/" + filename);
        if (resource.exists()) {
            return ResponseEntity.ok(resource);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}